﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace XYZshell.Game.CardGameEngine
{
	/// <summary>
	/// 基于对象属性的条件表达式
	/// </summary>
	[Expression( "property" )]
	public class PropertyExpression : BaseExpression
	{


		public object Left
		{
			get
			{
				ExpressionParam leftParam=Params["left"];
				return leftParam.Value;
			}
		
		}
		public object Right
		{
			get
			{
				ExpressionParam rightParam=Params["right"];
				return rightParam.Value;
			}
		}

		public ExpressionParam LeftParam
		{
			get
			{
				return Params["left"];
			}
			set
			{
				ExpressionParam p=value;
				p.Name = "left";
				Params["left"] = p;
			}
		}

		public ExpressionParam RightParam
		{
			get
			{
				return Params["right"];
			}
			set
			{
				ExpressionParam p=value;
				p.Name = "right";
				Params["right"] = p;
			}
		}
		/// <summary>
		/// 获得操作类型
		/// </summary>
		public OperatorType Operator
		{
			get
			{
				ExpressionParam op=Params["operator"];
				return (OperatorType)Enum.Parse( typeof( OperatorType ), op.ValueString );
				//switch (op.Value)
				//{
				//    case ">=":
				//        return OperatorType.GreaterThanOrEqual;
				//    case ">":
				//        return OperatorType.GreaterThan;
				//    case "=":
				//        return OperatorType.Equal;
				//    case "<":
				//        return OperatorType.LessThan;
				//    case "<=":
				//        return OperatorType.LessThanOrEqual;
				//}
				throw new Exception( "解析操作参数出错" );
			}
			set
			{
				ExpressionParam p=new ExpressionParam
				{
					Name="operator",
					ValueString=value.ToString()
				};
				Params["operator"] = p;
			}

		}


		/// <summary>
		/// 条件的值
		/// </summary>
		/// <returns></returns>
		public override bool Result()
		{
			IComparable i=null;
			switch (Operator)
			{

				case OperatorType.LessThan:
					i = (IComparable)Left;
					return i.CompareTo( Right ) < 0;
				case OperatorType.LessThanOrEqual:
					i = (IComparable)Left;
					return i.CompareTo( Right ) < 0 || i.CompareTo( Right ) == 0;
				case OperatorType.Equal:
					return Left == Right;
				case OperatorType.GreaterThanOrEqual:
					i = (IComparable)Left;
					return i.CompareTo( Right ) > 0 || i.CompareTo( Right ) == 0;
				case OperatorType.GreaterThan:
					i = (IComparable)Left;
					return i.CompareTo( Right ) > 0;
			}
			return false;
		}

		/// <summary>
		/// 从XML生成属性表达式
		/// </summary>
		/// <param name="xml"></param>
		/// <returns></returns>
		public static PropertyExpression FromXML( string xml )
		{
			try
			{
				PropertyExpression pe=new PropertyExpression();
				XObject xo=new XObject( xml );
				XObject pas=xo.Child( "params", true );
				foreach (XObject p in pas.Childs)
				{
					pe.Params.Add( ExpressionParam.FromXML( p.OuterXml ) );
				}
				return pe;
			}
			catch (Exception ex)
			{
				throw new Exception( ex.Message + "PropertyExpression:从XML中生成属性表达式出错" );
			}
		
		}
		///// <summary>
		///// 转换为XML
		///// </summary>
		///// <returns></returns>
		//public override string ToXML()
		//{
		//    throw new NotImplementedException();
		//}
	}
}
